package uk.co.droidinactu.sony.prst1.home.view;

import uk.co.droidinactu.sony.prst1.home.R;
import uk.co.droidinactu.sony.prst1.home.data.Book;
import uk.co.droidinactu.sony.prst1.home.data.DataModel;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

public final class BooksActivity extends AbstractListActivity implements OnItemClickListener, OnClickListener {

	private static final int[] bookColsMapTo = new int[] { R.id.book_grid_item_img,
			R.id.book_grid_item_title, R.id.book_grid_item_author };

	private static final String[] bookColumns = new String[] { BaseColumns._ID, Book.COLUMN_NAME_TITLE,
			Book.COLUMN_NAME_AUTHOR, };

	public static String SELECTED_COLLECTION_ID = "SELECTED_COLLECTION_ID";

	private GridView bkGridView;
	private long colctnId = -1;
	private String colctnName = "";
	private DataModel dataModel;
	private String filterChar = "";
	private LinearLayout indexBtns;
	private TextView txtSortOrdr;
	private TextView txtViewTitle;

	@Override
	public void onClick(final View v) {
		final String btnPressed = ((Button) v).getText().toString();
		filterChar = btnPressed;
		updateBooksGrid();
	}

	/** Called when the activity is first created. */
	@Override
	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.book_grid);

		final Intent intent = getIntent();
		final Bundle extras = intent.getExtras();
		if (extras != null) {
			colctnId = extras.getLong(SELECTED_COLLECTION_ID);
		}

		txtViewTitle = (TextView) findViewById(R.id.book_grid_txt_books);
		txtSortOrdr = (TextView) findViewById(R.id.book_grid_txt_sort_order);
		indexBtns = (LinearLayout) findViewById(R.id.book_grid_btn_index);

		indexBtns.removeAllViews();
		Button btn = new Button(this);
		btn.setOnClickListener(this);
		btn.setText("0");
		indexBtns.addView(btn);

		for (char ch = 'A'; ch <= 'Z'; ch++) {
			btn = new Button(this);
			btn.setOnClickListener(this);
			btn.setText("" + ch);
			indexBtns.addView(btn);
		}

		bkGridView = (GridView) findViewById(R.id.book_grid_books);
		bkGridView.requestFocus();
		bkGridView.setOnItemClickListener(this);
	}

	@Override
	public void onItemClick(final AdapterView<?> parent, final View view, final int position, final long id) {
		dataModel.launchBook(this, id);
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (dataModel == null) {
			dataModel = new DataModel(this);
		}
		if (colctnId > -1) {
			colctnName = dataModel.getCollection(this, colctnId).title;
			txtViewTitle.setText("Books (in " + colctnName + ")");
		}
		updateBooksGrid();
	}

	private void updateBooksGrid() {
		try {
			Cursor cursr = null;
			if (filterChar.length() > 0) {
				cursr = dataModel.getBooks(this, filterChar);
				txtViewTitle.setText("Books (" + filterChar + ")");
			} else if (colctnName.length() > 0) {
				cursr = dataModel.getBooksInCollection(this, colctnName);
				txtViewTitle.setText("Books (in " + colctnName + ")");
			} else {
				cursr = dataModel.getBooks(this);
			}
			if (cursr == null) {
				return;
			}
			final SimpleCursorAdapter tmpAdaptor = new SimpleCursorAdapter(this, R.layout.book_grid_item,
					cursr, bookColumns, bookColsMapTo);
			tmpAdaptor.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
				@Override
				public boolean setViewValue(final View view, final Cursor cursor, final int columnIndex) {
					boolean retval = false;
					final int idColIndex = cursor.getColumnIndexOrThrow(BaseColumns._ID);
					final int thumbColIndex = cursor.getColumnIndexOrThrow(Book.COLUMN_NAME_THUMBNAIL);

					if (columnIndex == idColIndex) {
						try {
							final ImageView coverImg = (ImageView) view;
							final String thumbnailFilename = cursor.getString(thumbColIndex);
							Bitmap bitmap = dataModel.getCoverImg(BooksActivity.this, thumbnailFilename);
							coverImg.setImageDrawable(new BitmapDrawable(bitmap));
							retval = true;
						} catch (final Exception e) {
						}
						retval = true;
					}
					return retval;
				}
			});
			bkGridView.setAdapter(tmpAdaptor);
			tmpAdaptor.notifyDataSetChanged();
		} catch (final Exception e) {
			Toast.makeText(this, "Exception Caught creating book grid", Toast.LENGTH_SHORT).show();
		}
	}
}
